<?php
namespace app\common\model;
use app\common\model\account\ShopAccountModel;
use app\common\model\CommonModel;
use app\common\model\AppModel;
use think\facade\Db;
class ShopModel extends CommonModel{
    protected  $name = 'shop';
    protected  $pk   = 'shop_id';
    protected $type_mean=[
        -1=>'取消授权',
        0=>'等待授权',
        1=>'已经授权',
        8=>'已经上线运营'
    ];
    public function application()
    {
        return $this->hasOne(AppModel::class,'app_id','app_id');
    }
    public function relation()
    {
        return $this->hasOne(ShopAccountModel::class,'shop_id','shop_id');//123
    }
    public function app()
    {
        return $this->hasOne(AppModel::class,'app_id','app_id');
    }
    public function kefu()
    {
        return $this->hasOne(CustomerServiceModel::class,'customer_service_id','customer_service_id');
    }
    public function brand()
    {
        return $this->hasOne(ShopBrandModel::class,'id','brand_id');
    }
    public function imglst()
    {
        return $this->hasMany(ShopImgModel::class,'shop_id','shop_id');
    }
    public function detail()
    {
        return $this->hasOne(ShopDetailModel::class,'shop_id','shop_id');
    }
    public function getStatusMeanAttr($value,$data)
    {
        return $this->type_mean[$data['status']] ?? '状态异常';
    }
    public function getExpireTimeFormatAttr($value,$data)
    {
        return date("Y-m-d",$data['expire_time']);
    }
    public function getIsExpireAttr($value,$data)
    {
        return $data['expire_time'] >= time() ? 0 : 1;
    }
    public static function shop_list($where,$order,$page=1,$limit=10){
        if(!isset($where['is_alliance'])){
            $where['is_alliance']=[0];
        }
        $query = Db::name('shop')->alias('a')
//            ->leftJoin('shop_alliance b','a.shop_id=b.shop_id')
            ->join('shop_detail d','a.shop_id=d.shop_id')
//            ->where(['a.is_alliance'=>0])
//            ->where(function ($query){
//                $query->whereNull('b.shop_id')->whereOr(['b.is_delete'=>1]);
//            })
//            ->whereNull('b.shop_id')
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(isset($where['is_alliance']),function ($query) use ($where){
                $query->where('a.is_alliance','in',$where['is_alliance']);
            })
            ->when(!empty($where['shop_name']),function ($query) use ($where){
                $query->where('a.name','like','%'.$where['shop_name'].'%');
            })
            ->field("a.name,a.shop_id,a.status,a.add_time,d.logo,d.address,d.tel,a.type,a.is_alliance");
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        if(!empty($list)){
            foreach ($list as &$v){
                $v['add_time']=date('Y-m-d H:i:s',$v['add_time']);
            }
        }
        return compact('count','list');
    }
    public static function up($data,$where){
        $res=self::update($data,$where);
        return $res;
    }
    public static function info($where){
        $info = self::when(isset($where['shop_id']),function ($query) use ($where){
                $query->where(['shop_id'=>$where['shop_id']]);
            })->with(['kefu'=>function($query){
                $query->where(['is_delete'=>0]);
            },'brand'=>function($query){
                $query->where(['is_delete'=>0]);
            },'detail','imglst'])
            ->field("shop_id,brand_id,name,customer_service_id")
            ->find();
        if(!empty($info['brand']) && $info['brand']['parent_id']>0){
            $parent=ShopBrandModel::info(['id'=>$info['brand']['parent_id']]);
            if(!empty($parent)){
                $info['brand']['parent_name']=$parent['name'];
            }
        }
        if(!empty($info['detail']) ){
            $info['detail']['business_time']=json_decode($info['detail']['business_time'],true);
        }
        return $info;
    }
    public  function getList(array $where,$filed='',$page,$limit,$order='sd.add_time desc'){
        $query = $this->alias('s')
            ->join('shop_detail sd','s.shop_id=sd.shop_id')
            ->where($where)
            ->field($filed);
        $count=$query->count();
        $list=$query->order($order)
            ->page($page,$limit)
            ->select();
        if($list){
            $list=$list->toArray();
            foreach ($list as &$item){
            }
        }
        return compact( 'count','list');
    }
}
